<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('机构管理列表')"/>
    <style>
        .layui-card-body .layui-form {
            margin: auto;
        }
        .layui-form-item {
            margin-bottom: 0px;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layui-row layui-col-space10">
    <div class="layui-col-md3">
        <div class="layui-card">
            <div class="layui-card-body" id="card2">
                <div class="main-container" style=" overflow: auto">
                    <ul data-id="0" id="deptTree" style="width: 100%;"></ul>
                </div>

            </div>
        </div>
    </div>
    <div class="layui-col-md9">
        <div class="layui-card">
            <div class="layui-card-body" id="card1">
                <form id="formId" class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">机构简称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nameAbbr" placeholder="请输入机构简称"
                                       class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">机构编码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" placeholder="请输入机构编码"
                                       class="layui-input"  lay-required/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-md" lay-submit lay-filter="dept-query">
                                <i class="layui-icon layui-icon-search"></i>
                                查询
                            </button>
                            <button type="reset" class="layui-btn layui-btn-md layui-btn-primary" id="formreset">
                                <i class="layui-icon layui-icon-refresh"></i>
                                重置
                            </button>
                        </div>
                    </div>
                </form>
                <table id="dept-table" lay-filter="dept-table"></table>
                <script type="text/html" id="dept-toolbar">
                    <button sec:authorize="hasPermission('/sys/dept/add','sys:dept:add')"
                            class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
                        <i class="layui-icon layui-icon-add-1"></i>
                        新增
                    </button>
                    <button sec:authorize="hasPermission('/sys/dept/import','sys:dept:import')"
                            class="layui-btn layui-btn-primary layui-btn-md" lay-event="import">
                        <i class="layui-icon layui-icon-upload"></i>
                        导入
                    </button>
                    <button sec:authorize="hasPermission('/sys/dept/export','sys:dept:export')"
                            class="layui-btn layui-btn-primary layui-btn-md" lay-event="export">
                        <i class="layui-icon layui-icon-download-circle"></i>
                        导出
                    </button>
                    <button sec:authorize="hasPermission('/sys/dept/remove','sys:dept:del')"
                            class="layui-btn layui-btn-danger layui-btn-md" lay-event="batchRemove">
                        <i class="layui-icon layui-icon-delete"></i>
                        删除
                    </button>
                </script>
                <script type="text/html" id="dept-bar">
                    <button sec:authorize="hasPermission('/sys/dept/edit','sys:dept:edit')"
                            class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit"><i
                            class="layui-icon layui-icon-edit"></i>
                    </button>
                    <button sec:authorize="hasPermission('/sys/dept/remove','sys:dept:del')"
                            class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i
                            class="layui-icon layui-icon-delete"></i>
                    </button>
                </script>
            </div>
        </div>
    </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script>
    var _formData,treeId;
    let table;
    layui.use(['table', 'form', 'jquery', 'dtree'], function () {
        table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;

        let prefix = pName + "/system/dept/";

        deptUtil.initUnitTree("deptTree", "1",
            function (deptId) {
                treeId = deptId;
                table.reload('dept-table', {where: {parentId: deptId}});
                return;
            },'人事'
        );

        let cols = [
            [
                {type: 'checkbox'},
                        {
                            field: 'id',
                            title: '机构主键',
                            hide: true
                        },
                        {
                            field: 'nameAbbr',
                            title: '机构简称'
                        },
                        {
                            field: 'code',
                            title: '机构编码'
                        },
                {title: '操作', toolbar: '#dept-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#dept-table',
            url: prefix + 'data',
            page: true,
            escape: true,
            cols: cols,
            skin: 'line',
            toolbar: '#dept-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            ,height: 'full-100'
            ,done: function () {
                $('#card2').height($('#card1').height());

                $(".main-container").height($('#card1').height() - 5);
            }
        });
        table.on('checkbox(dept-table)', function(obj){

        });
        table.on('row(dept-table)', function (obj) {

        });
        table.on('tool(dept-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(dept-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'import') {
                window.importDept();
            } else if (obj.event === 'export') {
                window.exportDept();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(dept-query)', function (data) {
            data.field.parentId = treeId;
            table.reload('dept-table', {where: data.field})
            _formData = data.field;
            return false;
        });

        window.add = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '新增机构管理',
                shade: 0.1,
                maxmin : true,
                area: ['99%', '99%'],
                content: prefix + 'add'
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }
        window.importDept = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '机构导入',
                shade: 0.1,
                maxmin : true,
                area: ['99%', '99%'],
                content: pName + '/system/common/importMain?dUrl=' +  encodeURIComponent(pName + '/system/file/download/deptImport') + '&uUrl=' + encodeURIComponent(pName + '/system/dept/upload')
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }
        window.exportDept = function () {
            console.log(JSON.stringify(_formData))
            let dUrl = prefix + "export";
            let formHtml = "";
            for(var k in _formData){
                formHtml += '<input name="'+k+'" value="'+_formData[k]+'" />';
            }
            $('<form id="dataForm" action="'+dUrl+'" method="post">' +
                formHtml +
                '</form>').appendTo('body').submit().remove();
            return false;
        }

        window.edit = function (obj) {
            let layerIndex = layer.open({
                type: 2,
                title: '修改机构管理',
                shade: 0.1,
                maxmin : true,
                area: ['99%', '99%'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该机构管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些机构管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('dept-table');
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('dept-table', {where: param});
        }

        $('#formreset').click(function(){
            $('#formId').find('input').val("")
        })
        $(window).on("resize", function () {
            $('#card2').height($('#card1').height());
            $(".main-container").height($('#card1').height() - 5);
        });
    })
</script>
</body>
</html>